package cn.sptpc.Controller;

import cn.sptpc.service.UserService;
import cn.sptpc.service.impl.UserServiceI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
    private UserService service = new UserServiceI();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf8");
        PrintWriter out = response.getWriter();
        String tran = request.getParameter("tran");
        if ("list".equals(tran)){
            String seach = request.getParameter("seach");
            if ("queryUser".equals(seach)){
                out.print(service.queryUser(request));
            }else {
                String allUser = service.getAllUser(request);
                out.print(allUser);
            }
        }else if ("select".equals(tran)){
            if ("info".equals(request.getParameter("get"))){
                if ("self".equals(request.getParameter("id"))){
                    out.println(service.getUserInfo(request));
                }else {
                    int id = Integer.parseInt(request.getParameter("id"));
                    out.println(service.getUserInfo(id));
                }
            }
        }else if ("del".equals(tran)){
            out.print(service.deleteUser(request));
        }else if ("add".equals(tran)){
            out.print(service.insertUser(request));
        }else if("face".equals(tran)){
            service.getUserFace(request,response);
        }else if("temp".equals(tran)){
            request.getSession().setAttribute("temp",request.getParameter("id"));
        }else if("updata".equals(tran)){
            out.print(service.updataUser(request));
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
